﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using FukiERP.Common.Constants;

namespace FukiERP.Helper {
    public class ValidateValue {
        public static bool ValidateRequirement(ErrorProvider epd, Control control) {
            if (control.Text == string.Empty) {
                epd.SetError(control, MessageConstants.RequiredErrorMessage);
                return false;
            }
            return true;
        }

        public static bool ValidateNumber(ErrorProvider epd, Control control) {
            int number;
            if (!string.IsNullOrEmpty(control.Text) && 
                !int.TryParse(control.Text,NumberStyles.Number,
                CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.LCID),out number)) {
                epd.SetError(control, MessageConstants.NumberFormatErrorMessage);
                return false;
            }
            return true;
        }

        public static bool ValidateNumberGrateThanZero(ErrorProvider epd, Control control) {
            int number = 0;
            if ((!string.IsNullOrEmpty(control.Text) &&
                !int.TryParse(control.Text, NumberStyles.Number,
                CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.LCID), out number)) ||
                number <= 0) {
                epd.SetError(control, MessageConstants.NumberFormatErrorMessage);
                return false;
            }
            return true;
        }

        public static bool ValidateDropDown(ErrorProvider epd, RadDropDownList control) {
            if (control.Text == CommonConstants.DefaultDropDownValue) {
                epd.SetError(control, MessageConstants.RequiredErrorMessage);
                return false;
            }
            return true;
        }

        public static bool ValidateRequirementGroup(ErrorProvider epd, params Control[] controls) {
            bool isValid = true;
            foreach (Control c in controls) {
                isValid = isValid && ValidateRequirement(epd, c);
            }
            return isValid;
        }

        public static bool ValidateNumberGroup(ErrorProvider epd, params Control[] controls) {
            bool isValid = true;
            foreach (Control c in controls) {
                isValid = isValid && ValidateNumber(epd, c);
            }
            return isValid;
        }
        public static bool ValidateNumberGrateThanZeroGroup(ErrorProvider epd, params Control[] controls) {
            bool isValid = true;
            foreach (Control c in controls) {
                isValid = isValid && ValidateNumberGrateThanZero(epd, c);
            }
            return isValid;
        }

        public static bool ValidateDropdownGroup(ErrorProvider epd, params RadDropDownList[] controls) {
            bool isValid = true;
            foreach (RadDropDownList c in controls) {
                isValid = isValid && ValidateDropDown(epd, c);
            }
            return isValid;
        }
    }
}
